package com.lu.security.cipher;

import org.jetbrains.annotations.NotNull;

import java.security.GeneralSecurityException;
import java.security.Key;

import javax.crypto.Cipher;

import com.lu.security.tools.HexTools;

/**
 * author: luqihua
 * date:2019-07-10
 * description: RSA
 **/
public class RSADecrypt {
    private static final String TAG = "RJCipher";

    private static final String RSA_MODEL = "RSA/ECB/PKCS1Padding";
    @NotNull
    private final Key mRSAKey;

    public RSADecrypt(@NotNull Key rsaKey) {
        mRSAKey = rsaKey;
    }

    public @NotNull byte[] decrypt(byte[] data) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(RSA_MODEL);
        cipher.init(Cipher.DECRYPT_MODE, mRSAKey);
        return cipher.doFinal(data);
    }

    public String decryptBase64(String base64Text) throws GeneralSecurityException {
        byte[] source = HexTools.decodeFromBase64(base64Text);
        return new String(decrypt(source));
    }
}
